package com.wondersgroup.insurance.datalibrary.Interceptor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/12/7.
 * head 拦截器
 */

public class HeaderInterceptor implements Interceptor {
    HashMap<String,String> headMap = new HashMap<>();
    public HeaderInterceptor() {
//           headMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    }

    public void addHeader(String key,String value){
        headMap.put(key, value);
    }


    public void removeHeader(String key){
        headMap.remove(key);
    }
    @Override public Response intercept(Interceptor.Chain chain) throws IOException {
        Request.Builder builder = chain.request()
                .newBuilder();
        if (headMap != null && headMap.size() > 0) {
            Iterator<String> keys = headMap.keySet().iterator();
            while(keys.hasNext()) {
                String key = (String) keys.next();
                String value=headMap.get(key);
                builder.addHeader(key,value).build();
            }
        }
        return chain.proceed(builder.build());
    }
}